import requests


class DownloadMusic(object):
    def __init__(self):
        self.song_url = 'http://music.163.com/song/media/outer/url?id={}'
        self.headers = {
            'user-agent': 'Mozilla5.0'
        }

    def get_song_url(self, song_id):
        """
        传入歌曲id，拼凑完整URL，然后调用下载音乐函数
        :param song_id: 歌曲id
        :return: 
        """
        url = self.song_url.format(song_id)
        self.download_music(url)

    def download_music(self, song_url):
        """
        接受歌曲URL，执行下载函数
        :param song_url: 歌曲URL
        :return: 
        """
        resp = requests.get(url=song_url)
        with open('Yellow.mp3', 'wb') as f:
            f.write(resp.content)

    def main(self):
        """
        主函数
        :return: 
        """
        self.get_song_url('1413502053')


if __name__ == '__main__':
    spider = DownloadMusic()
    spider.main()
